//
//  LTWIFITransportViewController.swift
//  WIFISend
//
//  Created by lete on 2020/7/20.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit


class LTWIFITransportViewController: UIViewController, GCDWebUploaderDelegate {
    
    var webServer:GCDWebUploader! = nil
    
    @IBOutlet weak var portTF: UITextField!
    
    @IBOutlet weak var adressL: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        
    }
    

    
    func initWebServer() {
        let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
//        let documentsPath = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, true).first
        print("文件存储位置 : \(documentsPath.path)")
        
        // 链接管理路径
        webServer = GCDWebUploader.init(uploadDirectory: documentsPath.path)
        
        webServer.delegate = self
        webServer.allowHiddenItems = true
        
        /// 托管的文件格式
        webServer.allowedFileExtensions = ["doc", "docx", "xls", "xlsx", "txt", "pdf","zip", "png"]
        /// 标题
        webServer.title = "乐特传输"
        /// 描述
        webServer.prologue = "欢迎使用 乐特 WIFI 管理平台"
        /// 版权
        webServer.epilogue = "lete制作";

        /// 开启服务和端口
//        webServer.start(withPort: 1234, bonjourName: "")  // 自定义端口
//        webServer.start() // 默认 80 端口
        
        var port:Int = 10000
        if self.portTF.text!.count > 0 {
            port = Int(self.portTF.text!)!
        }
        
        if  webServer.start(withPort: UInt(port), bonjourName: "mywifi") {
            print("请在网页输入这个地址   \(LTCSMIPHelper.deviceIPAdress() ?? ""):\(webServer.port)    \(String(describing: webServer.bonjourName))")
            self.adressL.text = LTCSMIPHelper.deviceIPAdress() + ":" + webServer.port.description
        }
        else{
            
        }
        
        
    }
    
    @IBAction func openServeBtn(_ sender: Any) {
        
        initWebServer()
    }
    /// 页面销毁时停止服务
    deinit {
        webServer.stop()
    }
    
    //MARK:  - <GCDWebUploaderDelegate>
    
    func webServerDidStop(_ server: GCDWebServer) {
        
    }
    /// 上传文件
    func webUploader(_ uploader: GCDWebUploader, didUploadFileAtPath path: String) {
        
    }
    /// 下载文件
    func webUploader(_ uploader: GCDWebUploader, didDownloadFileAtPath path: String) {
        
    }
    /// 创建文件夹
    func webUploader(_ uploader: GCDWebUploader, didCreateDirectoryAtPath path: String) {
        
    }
    /// 移动文件
    func webUploader(_ uploader: GCDWebUploader, didMoveItemFromPath fromPath: String, toPath: String) {
        
    }
    ///  删除文件
    func webUploader(_ uploader: GCDWebUploader, didDeleteItemAtPath path: String) {
        
    }
}
